package fr.umlv.escape_ig.game;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class UpdateThread {

	private final Thread thread;

	public UpdateThread(final SurfaceHolder holder, final GameState state) {

		this.thread = new Thread(new Runnable() {
			@Override
			public void run() {

				while (!Thread.currentThread().isInterrupted()) {

					Canvas canvas = holder.lockCanvas(null);

					state.computeNextFrame();
					state.drawFrame(canvas);

					holder.unlockCanvasAndPost(canvas);

					try {
						Thread.sleep(2);
					} catch (InterruptedException e) {
						break;
					}
				}
			}
		});
	}

	public void startThread() {
		thread.start();
	}

	public void stopThread() {
		thread.interrupt();
	}
}
